package com.qsign.sfrz_android.activity.home.ViewController;

import android.app.Dialog;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.lc_print_sdk.PrintConfig;
import com.example.print_sdk.PrintUtil;
import com.example.print_sdk.enums.ALIGN_MODE;
import com.example.print_sdk.enums.BARCODE_1D_TYPE;
import com.example.print_sdk.enums.MODE_ENLARGE;
import com.qsign.sfrz_android.R;
import com.qsign.sfrz_android.base.NewBaseActivity;
import com.qsign.sfrz_android.net.IServiceImpl;
import com.qsign.sfrz_android.net.RequestServiceImpl;
import com.qsign.sfrz_android.publicview.SimpleDialog;
import com.qsign.sfrz_android.utils.App;
import com.qsign.sfrz_android.utils.BaseUtil;
import com.qsign.sfrz_android.utils.SPUtils;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class KuweiActivity extends NewBaseActivity {
    @BindView(R.id.kueiedit)
    EditText kueiedit;

    private PrintUtil printUtil;
    com.example.lc_print_sdk.PrintUtil newprintUtil;
    int getsupportprint;

    private int number = 1000000001; // 初始化票据流水号 Initial bill serial number

    private final static String SCAN_ACTION = "scan.rcv.message";
    @Override
    protected void initTitle(TextView textView) {
        textView.setText("库位条形码打印");
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_kuwei;
    }

    @Override
    protected void onPause() {
        super.onPause();
        // unregisterReceiver(mScanReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        // registerReceiver(mScanReceiver, filter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        kueiedit.setFocusableInTouchMode(true);
        kueiedit.requestFocus();
        this.findViewById(R.id.nav_back).setVisibility(View.GONE);

        getsupportprint=(int) SPUtils.get (this, "getsupportprint", 0);
        if (getsupportprint == 0){
            printUtil = App.getPrint();
            printUtil.printEnableMark(true);
        }else {
            newprintUtil=App.getnewPrint ();
            newprintUtil.printEnableMark(true);
        }


    }

//    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if(tuopanedit.hasFocus()){
//                byte[] barocode = intent.getByteArrayExtra("barocode");
//                int barocodelen = intent.getIntExtra("length", 0);
//                String barcodeStr = new String(barocode, 0, barocodelen);
//                tuopanedit.setText(barcodeStr);
//                showWait();
//                RequestServiceImpl.tuopan_query(mMIPHandler, IServiceImpl.FH_QUERY,tuopanedit.getEditableText().toString());
//            }
//
//        }
//    };

    /**
     * 打印一维条码
     * @param barContent 条码内容
     */
    public void printBarcode(String barContent) {
        if (getsupportprint== 0){
            try {
                printUtil.printStartNumber(number);
                printUtil.printState();
                printUtil.printConcentration(10);
                printUtil.printFontSize(MODE_ENLARGE.HEIGHT_DOUBLE);
                printUtil.printAlignment(ALIGN_MODE.ALIGN_CENTER);
                printUtil.printLine(1);
                if (barContent.length() > 12) {
                    printUtil.printBarcode(barContent, BARCODE_1D_TYPE.CODE128, 140, 1);
                } else {
                    printUtil.printBarcode(barContent, BARCODE_1D_TYPE.CODE128, 140, 2);
                }
                printUtil.printGoToNextMark();
                printUtil.printEndNumber();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            newprintUtil.printConcentration(10);
            newprintUtil.printLine(1);
            newprintUtil.printFontSize(200);

            if (barContent.length() > 12) {
                newprintUtil.printBarcode(PrintConfig.Align.ALIGN_CENTER,140,barContent, BARCODE_1D_TYPE.CODE128.Get (), PrintConfig.HRIPosition.POSITION_NONE);
            } else {
                newprintUtil.printBarcode(PrintConfig.Align.ALIGN_CENTER,140,barContent, BARCODE_1D_TYPE.CODE128.Get (), PrintConfig.HRIPosition.POSITION_NONE);
            }
            newprintUtil.printLine(1);
            newprintUtil.printText(PrintConfig.Align.ALIGN_CENTER, PrintConfig.FontSize.TOP_FONT_SIZE_SUPER,false,false,barContent);
            newprintUtil.printGoToNextMark();

        }

    }

    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        switch (requestType) {
            case IServiceImpl.QUREYKUWEI://根据条件查询
                try {
                    JSONObject jsonObject = JSONObject.parseObject(objBody.toString());
                    msg = jsonObject.getString("MSG_INFO");
                    opt = jsonObject.getString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        ShowDialog();
                    } else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("查询出错");
                        } else {
                            showSimpleDialog(msg);
                        }
                        kueiedit.setText("");
                    }

                } catch (Exception e) {
                    showSimpleDialog(objBody.toString());
                }
                break;
        }
    }
    private void showSimpleDialog(String message) {
        SimpleDialog.show(this, message);
    }
    @OnClick({ R.id.nextbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nextbtn:
                if(kueiedit.getEditableText().toString().length()==0){
                    BaseUtil.ShowToast(this,"请输入库位条码");
                    return;
                }
                showWait();
                RequestServiceImpl.kuwei_query(mMIPHandler, IServiceImpl.QUREYKUWEI,kueiedit.getEditableText().toString());

                break;
        }
    }

    private void ShowDialog(){
        View viewTips = LayoutInflater.from(KuweiActivity.this).inflate(R.layout.print_num_dialog, null);
        AutoUtils.auto(viewTips);
        Dialog alertDialog = BaseUtil.ShowMYDialog(KuweiActivity.this, viewTips, 340);
        alertDialog.show();
        EditText numberedit = viewTips.findViewById(R.id.numberedit);

        viewTips.findViewById(R.id.printbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                closeKeyboard();
                if (numberedit.getEditableText().toString().length() == 0) {
                    BaseUtil.ShowToast(KuweiActivity.this, "请输入打印数量");
                } else {
                    int  pagenumbers = Integer.parseInt(numberedit.getEditableText().toString());
                    String tstr=kueiedit.getEditableText().toString();
                    kueiedit.setText("");
                    for (int i = 0; i < pagenumbers; i++) {
                        printBarcode(tstr);
                    }

                }

            }
        });
        viewTips.findViewById(R.id.cancellay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                closeKeyboard();
            }
        });
        numberedit.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showKeyboard(numberedit);
                } else {
                    closeKeyboard();
                }
            }
        });
    }
}